/**
 * Created by Littleconan on 2015/10/28.
 */
var order_no=new Date().getTime();
var flag=true;
$(function(){
        var sku=$('.product_sku span[data-price]');
        var price=$('.product_confirm span');
        var button=$(".product_confirm a");
        sku.eq(0).addClass("product_active");
        $.each(sku,function(){
            var _sku=$(this);
            _sku.on('click',function(){
                sku.removeClass("product_active");
                _sku.addClass("product_active");
                price.text("商品总价：￥"+_sku.attr("data-price")).css("display","inline");
                button.attr("href","../buy/"+_sku.attr("data-price"));
            });
        });

        $(".small").bind("mouseover", function() {
            var src = $(this).attr("src");
            $("#spec-n1 img").eq(0).attr({
                src: src.replace("\/n5\/", "\/n1\/"),
                jqimg: src.replace("\/n5\/", "\/n0\/")
            });
            $(this).css({
                "border": "2px solid #ff6600",
                "padding": "1px"
            });
        }).bind("mouseout", function() {
            $(this).css({
                "border": "1px solid #ccc",
                "padding": "2px"
            });
        });
        //商品图片放大代码
        $(".jqzoom").jqueryzoom({
            xzoom: 400,
            yzoom: 400,
            offset: 10,
            position: "right",
            preload: 1,
            lens: 1
        });
    $("#buy").on("click",function(){
        _category=$(this).data("cate");
        _id=$(this).data("id");
        _url=$(this).data("url");
        $.ajax({
            type:'get',
            url:'/check/check/',
            dataType:'json',
            success:function(data){
                if(data==0)
                {
                    alert("请先登陆！");
                    return;
                }
                else
                {
                    if($(".product_sku span").eq(0).data('price')==undefined)
                    {
                        
                        $.ajax({
                            type:"get",
                            url:"/qrcode/",
                            data:{data:$('.product_price span').text(),order_no:order_no,id:$(".product_name").data("id")},
                            dataType:"html",
                            success:function(data){
                                // alert(data);
                                $(".qrcode").html("");
                                $(".qrcode").append(data);
                            }
                        });
                        // $(".qrcode").append("/qrcode/?data="+$('.product_price span').text()+"&order_no="+order_no+"&id="+$(".product_name").data("id"));
                    }
                    else
                    {
                        // $(".qrcode").parent().html("");
                         $.ajax({
                            type:"get",
                            url:"/qrcode/",
                            data:{data:$('.product_sku').find(".product_active").attr("data-price"),order_no:order_no,id:$(".product_name").data("id")},
                            dataType:"html",
                            success:function(data){
                                // alert(data);
                                $(".qrcode").html("");
                                $(".qrcode").append(data);
                            }
                        });
                        // $(".qrcode").append("/qrcode/?data="+$('.product_sku').find(".product_active").attr("data-price")+"&order_no="+order_no+"&id="+$(".product_name").data("id"));
                    }
                    // $(".qrcode").attr("src","/qrcode/?data="+$('.product_sku').find(".product_active").attr("data-price")+"&order_no="+order_no+"&id="+$(".product_name").data("id"));
                    $("#qrcode").modal();
                    var check=setInterval(function(){
                        if(!flag)
                        {
                            clearInterval(check);
                        }
                        else
                        {
                            $.ajax({
                                method:"post",
                                url:"/notify/",
                                data:{"no":order_no},
                                dataType:"json",
                                success: function (data) {
                                    if(data )
                                    {
                                        if(_id==1 || _category!=2)
                                        {
                                            $(".modal-title").text("支付成功，请前往后台查看");
                                            flag=false;
                                        }
                                        else if(_category==2&&_id!=1)
                                        {
                                            $(".modal-title").text("支付成功");
                                            flag=false;
                                            location.href="/index/product/system/"+_url;
                                        }
                                    }
                                    else
                                    {
                                        console.log(2);
                                    }
                                }
                            });
                        }
                    },1000);
                }
            },
            error:function(){
                alert('error');
                return;
            }
        })
    });
});
